//
//  CashbackRecordDetailVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/6/12.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "CashbackRecordDetailVC.h"

#import "CashbackRecordStore.h"
#import "CashbackRecordModel.h"

#import "YHBToastView.h"

@interface CashbackRecordDetailVC ()

@property (nonatomic, strong) PushView *msgView;
@property (nonatomic, strong) UIView *SureView;

@property (weak, nonatomic) IBOutlet UILabel *Applyer;
@property (weak, nonatomic) IBOutlet UILabel *ApplyerPhone;
@property (weak, nonatomic) IBOutlet UILabel *minmoney;
@property (weak, nonatomic) IBOutlet UILabel *maxmoney;
@property (weak, nonatomic) IBOutlet UILabel *snnumber;
@property (weak, nonatomic) IBOutlet UILabel *StateLabel;
@property (weak, nonatomic) IBOutlet UILabel *ApplyerTime;
@property (weak, nonatomic) IBOutlet UILabel *ApplyState;


@end

@implementation CashbackRecordDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self loadData];
}
- (void)initCSS{
    [self setNavBarWithTitle:@"我接收的"];
}
- (void)loadData{
    [[[CashbackRecordStore alloc]init] GetDetailWithResetRecordId:self.resetRecordId Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            CashbackRecordModel *model = [CashbackRecordModel mj_objectWithKeyValues:res[@"data"]];
            model.backAmount = [NSString stringWithFormat:@"%@",res[@"data"][@"newAmount"]];
            [self ClerateViewWith:model];
        }else{
            [self showMBPError:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)ClerateViewWith:(CashbackRecordModel *)model{
    self.Applyer.text = model.requesterName;
    self.ApplyerPhone.text = model.requestPhone;
    self.minmoney.text = model.sourceAmount;
    self.maxmoney.text = model.backAmount;
    self.snnumber.text = model.sn;
    self.StateLabel.text = model.category;
    self.ApplyerTime.text = model.createTime;
    if ([model.status isEqualToString:@"1"]) {//1：待处理；2：已同意；3：已拒绝；4：已过期
        self.ApplyState.textColor = [UIColor colorWithHexString:@"0CA6F0"];
        self.ApplyState.text = @"待处理";
        [self addSureButton];
    }else if ([model.status isEqualToString:@"2"]) {
        self.ApplyState.textColor = [UIColor colorWithHexString:@"189633"];
        self.ApplyState.text = @"已同意";
    }else if ([model.status isEqualToString:@"3"]) {
        self.ApplyState.textColor = [UIColor colorWithHexString:@"FF2929"];
        self.ApplyState.text = @"已拒绝";
    }else if ([model.status isEqualToString:@"4"]) {
        self.ApplyState.textColor = [UIColor colorWithHexString:@"999999"];
        self.ApplyState.text = @"已过期";
    }
}
- (void)addSureButton{
    self.SureView = [[UIView alloc]init];
    self.SureView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.SureView];
    [self.SureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.left.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(- iPhoneX_Bottom);
    }];
    for (NSInteger i = 0; i < 2; i++) {
        UIButton *button = [[UIButton alloc]init];
        [button addTarget:self action:@selector(SureAndCancel:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 3245 + i;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        if (i == 0) {
            button.backgroundColor = [UIColor colorWithHexString:@"B3B3B3"];
            [button setTitle:@"拒绝" forState:UIControlStateNormal];
        }
        if (i == 1) {
            button.backgroundColor = [UIColor colorWithHexString:@"0CA6F0"];
            [button setTitle:@"同意" forState:UIControlStateNormal];
        }
        [self.SureView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.equalTo(self.SureView.mas_left).offset(0);
                make.width.mas_equalTo(0.4 * SCREEN_WIDTH);
            }
            if (i == 1) {
                make.left.equalTo(self.SureView.mas_left).offset(0.4 * SCREEN_WIDTH);
                make.right.equalTo(self.SureView.mas_right);
            }
            make.height.mas_equalTo(44);
            make.top.equalTo(self.SureView.mas_top);
        }];
    }
}
- (void)SureAndCancel:(UIButton *)button{
    if (button.tag - 3245 == 0) {
        [self loadSureOrCancel:@"2"];
    }
    if (button.tag - 3245 == 1) {
        [self loadSureOrCancel:@"1"];
    }
}
- (void)loadSureOrCancel:(NSString *)type{
    [[[CashbackRecordStore alloc]init] CancelOrSureWithResetRecordId:self.resetRecordId AndType:type Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.msgView = [[PushView alloc]initWithCashbackAlertWithType:[type integerValue]];
            [self.view addSubview:self.msgView];
            [self.msgView setAlertsureBlock:^{
                [[AppDelegate sharedAppDelegate] popViewController];
            }];
        }else{
            [self showMBPError:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
@end
